 import * as echarts from '../../ec-canvas/echarts';
//图表开始
var app = getApp();
var XList = [];
var YList = [];
let chart = null;//拿出来作为全局变量
var option = {    //拿出来作为全局变量
  title: {
    text: '主标题',
    subtext: '副标题',
    x: 'center',
    align: 'right'
  },
  toolbox: {
    feature: {
      restore: {},
      //magicType: { type: ['line', 'bar'] },
    }
  },
  tooltip: {
    trigger: 'axis',
    axisPointer: {            // 坐标轴指示器，坐标轴触发有效
      type: 'line'        // 默认为直线，可选为：'line' | 'shadow'
    }
  },
  grid: {
    left: 40,
    bottom: 75,
    show: true,
  },
  legend: {
    data: ['x'],
    x: 'left'
  },
  dataZoom: [{
    type: 'inside',
    start: 0,
    end: 100
  }, {
    handleIcon: 'M10.7,11.9v-1.3H9.3v1.3c-4.9,0.3-8.8,4.4-8.8,9.4c0,5,3.9,9.1,8.8,9.4v1.3h1.3v-1.3c4.9-0.3,8.8-4.4,8.8-9.4C19.5,16.3,15.6,12.2,10.7,11.9z M13.3,24.4H6.7V23h6.6V24.4z M13.3,19.6H6.7v-1.4h6.6V19.6z',
    handleSize: '90%',
    handleStyle: {
      color: '#fff',
      shadowBlur: 3,
      shadowColor: 'rgba(0, 0, 0, 0.6)',
      shadowOffsetX: 2,
      shadowOffsetY: 2
    }
  }],
  xAxis: [
    {
      type: 'category',
      boundaryGap: false,
      axisLine: { onZero: false },
      data: YList
    }
  ],
  yAxis: [
    {
      name: '分数',
      type: 'value',

    }
  ],
  //series 曲线系列
  series: [{
    name: 'A',
    type: 'line',
    smooth: true,

    data: XList
  }, 
  /*
  {
    name: 'B',
    type: 'line',
    smooth: true,
    //   data: [12, 50, 51, 35, 70, 30, 20]
  },
 
   {
    name: 'C',
    type: 'line',
    smooth: true,
   // data: [10, 30, 31, 50, 40, 20, 10]
  }
   */
  ]
};

function initChart(canvas, width, height) {
  chart = echarts.init(canvas, null, {
    width: width,
    height: height
  });
  canvas.setChart(chart);
  var option1 = option;   //option 是上面预先的赋值
 
  chart.setOption(option1);
  console.log('时间2')
  return chart;
}


//图表结束


Page({

  /**
   * 页面的初始数据
   */
  data: {
    
    ec: {
      onInit: initChart
    }
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     console.log('show页的'+ options.danxiang)
    var danxiang = options.danxiang
    wx.request({
      url: 'https://api.bjlya.com.cn/nfq.php/index/Pusa/index5', // 仅为示例，并非真实的接口地址
     
      data: {
        openid: app.openid,
        danxiang: danxiang,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: (res) => {
        // 这里不能删除之前的数据 for 执行完之后 才执行这里
        for (var i in res.data['tu']['age1']) {
          // for (var i = 0; i < res.length; i++) {
          XList.push(res.data['tu']['age1'][i]);
         // YList.push("25,48,39");
        }
        for (var i in res.data['tu']['name']) {
          // for (var i = 0; i < res.length; i++) {
          YList.push(res.data['tu']['name'][i]);

          // console.log(res.data.name);
        }
        console.log(XList);
        console.log(YList);
        XList = []
        chart.setOption(option); //不写就无法赋值
       // 
        console.log('onload验证')
        console.log(option)
      },
      fail: function (res) { },
      complete: function (res) { },
    })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    console.log('onReady')

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
console.log('隐藏')
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    console.log('卸载')
    /*
    console.log('卸载')
    chart.setOption(option); //不写就无法赋值
    console.log(XList);
    console.log(option.xAxis['0']['data']);
    option.xAxis['0']['data'] = []
    option.yAxis['0']['data'] = []
    console.log(option.yAxis['0']['data']);
     XList = [];   //关闭 就没有数据了
     YList = [];
 
    */
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})